/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.iti.jets.gpts.service;

import com.iti.jets.gpts.pojo.Comments;
import com.iti.jets.gpts.pojo.TranService;
import com.iti.jets.gpts.pojo.User;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

/**
 *
 * @author YASMIN-ITI
 */
public class UserCommentService {

    private WebResource webResource;
    private Client client;
    private static final String BASE_URI = "http://localhost:48651/GPTRP/rest";

    public UserCommentService() {
        com.sun.jersey.api.client.config.ClientConfig config = new com.sun.jersey.api.client.config.DefaultClientConfig();
        client = Client.create(config);
        webResource = client.resource(BASE_URI).path("user");
    }

    private List<Comments> getAllComments() {
        List<Comments> commentses = new ArrayList<Comments>();
        WebResource resource = webResource;
        resource = resource.path("getallcomment");
        JSONArray array = resource.accept(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(JSONArray.class);
        for (int i = 0; i < array.length(); i++) {
            JSONObject jSONObject = new JSONObject();
            try {
                User user = new User();
                TranService transervice = new TranService();
                jSONObject = array.getJSONObject(i);
                Comments comm = new Comments();
                comm.setId(jSONObject.getInt("id"));
                comm.setComment(jSONObject.getString("comment"));

                user.setId(jSONObject.getInt("userID"));
                user.setEmail(jSONObject.getString("userEmail"));
                comm.setUser(user);

                transervice.setId(jSONObject.getInt("transerviceID"));
                transervice.setName(jSONObject.getString("transerviceName"));
                comm.setTranService(transervice);

                commentses.add(comm);
            } catch (JSONException ex) {
                ex.printStackTrace();
            }
        }
        return commentses;
    }

    public List getCommnetUser(int id) {
        System.out.println(id);
        List<Comments> commentses = new ArrayList<Comments>();
        List<Comments> Temp = getAllComments();
        int numberOfComments = Temp.size();
        System.out.println("ooooooooooooooooooooooooooooo"+numberOfComments);
        for (int i = 0; i < numberOfComments; i++) {
            if (Temp.get(i).getUser().getId() == id) {
                commentses.add(Temp.get(i));
            }
        }
        return commentses;
    }

}
